package com.ambimmort.prserver.repository;

import com.ambimmort.prserver.repository.webservice.PRMWebService;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.ambimmort.prserver.repository.webservice.PRMWebServiceImpl;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
 *
 * @author 定巍
 */
public class SVNServer {

    private static SVNServer instance = null;

    private SVNServer() {
        Repository.init();
    }

    public static SVNServer getInstance() {
        if (instance == null) {
            instance = new SVNServer();
        }
        return instance;
    }

    private Server server = null;

    public boolean start() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            factory.setServiceClass(PRMWebService.class);
            factory.setServiceBean(new PRMWebServiceImpl());
            factory.setAddress("http://" + addr.getHostAddress() + ":9004");
            server = factory.create();
            server.start();
            return true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch(RuntimeException e){
            e.printStackTrace();
            return false;
        }

    }

    public boolean stop() {
        if (server == null) {
            return false;
        } else {
            if (server.isStarted()) {
                server.stop();
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean isStarted() {
        if (server == null) {
            return false;
        }
        return server.isStarted();
    }

}
